package com.smh.Orika;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;

import java.util.*;

/**
 * @description:使用orika实现类映射
 * @author: shenmhk
 * @create: 2020-10-19 16:01
 */
public class TestOrika {

    public static void main(String[] args) {

        //创建实例对象
        Map<String,String> address1 = new HashMap<>();
        address1.put("company","用友");
        address1.put("home","北京");
        Map<String,String> address2 = new HashMap<>();
        address2.put("company","用友");
        address2.put("home","郑州");

        Person person1 = new Person();
        person1.setId(1200L);
        person1.setName("小明");
        person1.setEmail("xiaoming@163.com");
        person1.setBirth(new Date());
        person1.setUser(new User(18,"程序员"));
        person1.setAddress(address1);
        Person person2 = new Person(1100L,"小红","xiaohong@163.com",new Date(),new User(16,"老师"),address2);

        /**
         * 11、对象之间默认字段映射
         */
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
//        mapperFactory.classMap(person1.getClass(),PersonDto.class)
//                .fieldAToB("user.age","age")
//                .fieldAToB("user.role","role")
//                .fieldAToB("address['company']","company")//添加映射map
//                .field("address['home']","home");
//                .byDefault().register();
//
// 排除指定的某个字段不用映射，在classMap()方法后直接调用exclude("指定字段")即可。
        ClassMapBuilder classMapBuilder =  mapperFactory.classMap(person2.getClass(),PersonDto.class)
                .fieldAToB("user.age","age")
                .field("user.role","role")
                .fieldAToB("address['company']","company")//添加映射map
                .field("address['home']","home");
        classMapBuilder.byDefault().register();



        //使用原始方法
//        MapperFacade mapper = mapperFactory.getMapperFacade();//1、
//        PersonDto dto = mapper.map(person1,PersonDto.class);
//        PersonDto personDto = mapperFactory.getMapperFacade().map(person1,PersonDto.class);//2、

        /**
         * 映射为集合
         */
        List<Person> personList = new ArrayList<>();
        personList.add(person1);
        personList.add(person2);
        //使用原始方法
        List<PersonDto> list = mapperFactory.getMapperFacade().mapAsList(personList,PersonDto.class);


        /*
         * 22、如何自定义字段映射，DO和DTO之间字段有着明显的差异，如果通过属性获取方式来映射，这就得说起Orika的Field属性了fieldAToB
         * */
        Map<String,String> map = new HashMap<>();
        map.put("birth","birth");
        map.put("user.age","age");
        map.put("user.role","role");
        map.put("address['company']","company");
        map.put("address['home']","home");
        // 嵌套映射,通过对象.属性获取到对应的字段

        //使用单例
        PersonDto personDto = ClassMapUtils.INSTANCE.map(PersonDto.class,person2,map);
        System.out.println("对象之间默认字段映射:"+personDto);

        //使用单例
//        List<PersonDto> list = ClassMapUtils.INSTANCE.mapAsList(PersonDto.class,personList,map);
        System.out.println("映射为集合："+list);

    }

}
